package org.breizhjug.cxf2jmeter;

import java.io.StringWriter;
import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.lang.StringEscapeUtils;
import org.w3c.dom.Document;

public class Message
{
    public enum Type
    {
        INBOUND, OUTBOUND
    }

    private Type type;

    private String id;

    private String address;

    private Map<String, String> headers;

    private Document payload;

    public Type getType()
    {
        return type;
    }

    public String getFormattedPayload()
        throws Exception
    {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
        transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "3" );

        StreamResult result = new StreamResult( new StringWriter() );
        DOMSource source = new DOMSource( payload );
        transformer.transform( source, result );
        return StringEscapeUtils.escapeXml( result.getWriter().toString() );
    }

    public void setType( Type type )
    {
        this.type = type;
    }

    public String getId()
    {
        return id;
    }

    public void setId( String id )
    {
        this.id = id;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress( String address )
    {
        this.address = address;
    }

    public Map<String, String> getHeaders()
    {
        return headers;
    }

    public void setHeaders( Map<String, String> headers )
    {
        this.headers = headers;
    }

    public Document getPayload()
    {
        return payload;
    }

    public void setPayload( Document payload )
    {
        this.payload = payload;
    }
}
